package com.school.service.Impl;

import com.school.common.ServiceResultEnum;
import com.school.controller.vo.IndexCategoryVO;
import com.school.controller.vo.IndexConfigGoodsVO;
import com.school.entity.Goods;
import com.school.entity.IndexConfig;
import com.school.mapper.GoodsMapper;
import com.school.mapper.IndexConfigMapper;
import com.school.mapper.OrderMapper;
import com.school.service.IndexConfigService;
import com.school.util.BeanUtil;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IndexConfigServiceImpl implements IndexConfigService {
    @Resource
    private IndexConfigMapper indexConfigMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public PageResult getConfigsPage(PageQueryUtil pageQueryUtil) {
        // 查询总条数
        int totalCount = indexConfigMapper.getTotalIndexConfigCount(pageQueryUtil);
        // 查询当前页数据
        List<IndexConfig> indexConfigs = indexConfigMapper.findIndexConfigList(pageQueryUtil);
        // 构建并返回 PageResult 对象
        PageResult pageResult = new PageResult(indexConfigs, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public String updateIndexConfig(IndexConfig indexConfig) {
        // 查询一下有没有这个商品
        IndexConfig temp = indexConfigMapper.selectByPrimaryKey(indexConfig.getConfigId());
        if (temp == null) {
            // 如果没有找到对应的记录，返回数据不存在的结果
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }

        // 检查是否存在相同商品ID和配置类型的记录（排除当前记录本身）
        IndexConfig temp2 = indexConfigMapper.selectByGoodsIdAndType(indexConfig.getGoodsId(), indexConfig.getConfigType());
        if (temp2 != null && temp2.getConfigId().longValue() != indexConfig.getConfigId().longValue()) {
            // 如果存在相同的配置，返回相同索引配置已存在的结果
            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
        }

        // 设置更新时间为当前时间
        indexConfig.setUpdateTime(new Date());

        // 更新数据库中的记录
        if (indexConfigMapper.updateByPrimaryKeySelective(indexConfig) > 0) {
            // 如果更新成功，返回成功的結果
            return ServiceResultEnum.SUCCESS.getResult();
        }

        // 如果更新失败，返回数据库错误的结果
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String saveIndexConfig(IndexConfig indexConfig) {
        // 查询一下有没有这个商品
        // 查询是否已经存在当前的热销新品 为你推荐的
        IndexConfig indexConfig1 = indexConfigMapper.selectByGoodsIdAndType(indexConfig.getGoodsId(), indexConfig.getConfigType());
        if (indexConfig1 != null) {
            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
        }
        int count = indexConfigMapper.insertSelective(indexConfig);
        if (count > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public List<IndexConfigGoodsVO> getConfigGoodsForIndex(int configType, int number) {
        List<IndexConfigGoodsVO> indexConfigGoodsVOS = new ArrayList<>();
        List<IndexConfig> indexConfigs = indexConfigMapper.findIndexCpnfigByTypeAndNum(configType, number);
        if (indexConfigs != null) {
            List<Long> goodsIds = new ArrayList<>();
            for (IndexConfig indexConfig : indexConfigs) {
                goodsIds.add(indexConfig.getGoodsId());
            }
            List<Goods> goods = goodsMapper.selectByPrimaryKeys(goodsIds);
            indexConfigGoodsVOS = BeanUtil.copyList(goods, IndexConfigGoodsVO.class);
            for (IndexConfigGoodsVO indexConfigGoodsVO : indexConfigGoodsVOS) {
                String goodsName = indexConfigGoodsVO.getGoodsName();
                String goodsIntro = indexConfigGoodsVO.getGoodsIntro();
                if (goodsName.length() > 30) {
                    goodsName = goodsName.substring(0, 30) + "...";
                    indexConfigGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 20) {
                    goodsIntro = goodsIntro.substring(0, 20) + "...";
                    indexConfigGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        return indexConfigGoodsVOS;
    }



}

